﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CivilleWP7.TaskServiceReference;

namespace CivilleWP7
{
    public partial class MoreTasks : PhoneApplicationPage
    {
        public MoreTasks()
        {
            InitializeComponent();
            GetTasksForLocation(46.7566, 23.5920);
        }
        private void GetTasksForLocation(double lat, double lang)
        {
            //We can call the Task Service for getting the current tasks in
            //a given radius around the location. For now we call it without the location param.
            //This should be renamed to getTasksAroundRadius(lat, long, radius) or something similar.

            TaskServiceReference.TaskServiceClient taskService =
                new TaskServiceReference.TaskServiceClient();
            //cookieContainer = new CookieContainer();
            //taskService.CookieContainer = cookieContainer;
            taskService.GetAllTasksCompleted +=
              new EventHandler<TaskServiceReference.GetAllTasksCompletedEventArgs>(TaskService_GetTasksCompleted);
            taskService.GetAllTasksAsync();
        }
        void TaskService_GetTasksCompleted(object sender, TaskServiceReference.GetAllTasksCompletedEventArgs e)
        {
            List<TaskData> tasks = e.Result;
            //MessageBox.Show("Just received " + tasks.Count + " tasks.");
            
            foreach (TaskData task in tasks)
            {
                if(task.Task.Type.Equals("typ1")) 
                    TaskCat1List.Items.Add(task);
                else
                    TaskCat2List.Items.Add(task);
            }
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            var curr_Task = (Task)sender;
            var name = (string)curr_Task.Name;
            NavigationService.Navigate(new Uri("/Task.xaml?id="+curr_Task.ID, UriKind.Relative)); 
        }
    }
}